//
//  GYRootViewController.m
//  research
//
//  Created by 章鱼 on 17/3/27.
//  Copyright © 2017年 simon. All rights reserved.
//

#import "GYRootViewController.h"
#import "GYBaseNavViewController.h"
#import "GYHomeViewController.h"
#import "GYQuestionViewController.h"
#import "GYMineViewController.h"
#import "GYNewsViewController.h"

#define IS_IPHONE_6_LATER (IS_IPHONE && SCREEN_MAX_LENGTH > 568.0)

@interface GYRootViewController ()<
UITabBarControllerDelegate
>

{
    GYBaseNavViewController *_homeNav;
    GYBaseNavViewController *_questionNav;
    GYBaseNavViewController *_newsNav;
    GYBaseNavViewController *_mineNav;
    
    GYHomeViewController *_homeVC;
    GYQuestionViewController *_questionVC;
    GYNewsViewController *_newsVC;
    GYMineViewController *_mineVC;
}

@end

@implementation GYRootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    [self setTabbarItem];
    [self setupTabBarBackgroundImage];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillLayoutSubviews {
    
    CGRect tabFrame = self.tabBar.frame;
    tabFrame.size.height = 49;
    tabFrame.origin.y = self.view.frame.size.height - 49;
    self.tabBar.frame = tabFrame;
}


- (void)setupTabBarBackgroundImage {
    UIImage *image = [UIImage imageNamed:@"tabbare_background"];
    CGFloat top = 40; // 顶端盖高度
    if (!IS_IPHONE_6_LATER) {
        top = 140; // 顶端盖高度
    }
    
    
    CGFloat bottom = 40 ; // 底端盖高度
    
    CGFloat left = 100; // 左端盖宽度
    CGFloat right = 100; // 右端盖宽度
    UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
    
    UIImage *TabBgImage = [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
    self.tabBar.backgroundImage = TabBgImage;
    
    [[UITabBar appearance] setShadowImage:[UIImage new]];
    [[UITabBar appearance] setBackgroundImage:[[UIImage alloc]init]];
}
- (void)setTabbarItem {
    
    //首页
    _homeVC = [[GYHomeViewController alloc] init];
    _homeNav = [[GYBaseNavViewController alloc] initWithRootViewController:_homeVC];
    _homeNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"推荐"
                                                        image:[[UIImage imageNamed:@"tabbare_home-page_normal"]
                                                               imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                                                selectedImage:[UIImage imageNamed:@"tabbare_home-page_selected"]];
    //问卷
    _questionVC = [[GYQuestionViewController alloc] init];
    _questionNav = [[GYBaseNavViewController alloc] initWithRootViewController:_questionVC];
    _questionNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"问卷"
                                                        image:[[UIImage imageNamed:@"tabbare_questionnaire_normal"]
                                                               imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                                                selectedImage:[UIImage imageNamed:@"tabbare_questionnaire_selected"]];
    //新闻
    _newsVC = [[GYNewsViewController alloc] init];
    _newsNav = [[GYBaseNavViewController alloc] initWithRootViewController:_newsVC];
    _newsNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"新闻"
                                                        image:[[UIImage imageNamed:@"tabbare_news_normal"]
                                                               imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                                                selectedImage:[UIImage imageNamed:@"tabbare_news_selected"]];

    //个人中心
    _mineVC = [[GYMineViewController alloc] init];
    _mineNav = [[GYBaseNavViewController alloc] initWithRootViewController:_mineVC];
    _mineNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的"
                                                        image:[[UIImage imageNamed:@"tabbare_my_normal"]
                                                               imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                                                selectedImage:[UIImage imageNamed:@"tabbare_my_selected"]];

    NSArray *controllers = @[_homeNav,_questionNav,_newsNav, _mineNav];
    [self setViewControllers:controllers];
    
    [[UITabBar appearance] setShadowImage:[UIImage new]];
    [[UITabBar appearance] setBackgroundImage:[[UIImage alloc]init]];
}



@end
